#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <cstring>
using namespace std;


struct Node
{
    int val;
    vector<int> chlidren;
    Node(int v):val(v) {};
    Node():val(0) {}
};
 
class UnSet
{
public:
    int *father;
 
    UnSet(int n): capacity(n)
    {
        father = new int[n];
        for (int i = 0; i < n ;i++)
            father[i] = i;
    }
 
    ~UnSet()
    {
        delete[] father;
    }
 
    int find(int i)
    {
        return (father[i] == i) ? i : (father[i] = find(father[i]));
    }
 
    void unionSet(int a, int b)
    {
        a = find(a);
        b = find(b);
        father[b] = a;
    }
 
    int size()
    {
        return capacity;
    }
 
private:
    int capacity;
};
 
class LCA
{
public:
    LCA(Node tree[], int n): fatSet(n) //root is tree[0]
    {
        hasVisit = new bool[n];
        memset(hasVisit, false, n * sizeof(bool));
        this->tree = tree;
    }
 
    map<pair<int, int>, int> calLCA(vector<pair<int, int> >& query)
    {
        map<int, set<int> > queryMap;
        map<pair<int, int>, int> result;
        for (int i =0; i< query.size(); i++)
        {
            int u = query[i].first;
            int v = query[i].second;
            set<int> temp;
            if (queryMap.count(u) == 0)
                queryMap[u] = temp;
            if (queryMap.count(v) == 0)
                queryMap[v] = temp;
            queryMap[u].insert(v);
            queryMap[v].insert(u);
        }
        tarjan(0, queryMap, result);
        return result;
    }
 
    void tarjan(int u, map<int, set<int> >& queryMap, map<pair<int, int>, int>& result)
    {
        fatSet.father[u] = u;
        hasVisit[u] = true;
        for (set<int>::iterator it = queryMap[u].begin(); it != queryMap[u].end(); it++)
        {
            int v = *it;
            if (hasVisit[*it])
            {
                pair<int,int> temp(v, u);
                result[temp] = fatSet.find(v);
            }
        }
        for (int i = 0; i < tree[u].chlidren.size(); i++)
        {
            int v = tree[u].chlidren[i];
            if (false == hasVisit[v])
            {
                tarjan(v, queryMap, result);
                fatSet.unionSet(u, v);
            }
        }
    }
 
private:
    bool *hasVisit;
    UnSet fatSet;
    Node *tree;
};
 
 
 
int main()
{
	
	int num_nodes = 0, x=0 ,y=0;
	int num_query = 0;
	cin>>num_nodes;
    Node testTree[num_nodes+1] ;

	testTree[0].chlidren.push_back(1); 
	
	
    for(int i = 1; i < num_nodes; i++)
    {
        cin>>x>>y;
        testTree[x].chlidren.push_back(y);  
    }
	
	
//	testTree[1].chlidren.push_back(2);  
//	testTree[1].chlidren.push_back(3);  
//	testTree[1].chlidren.push_back(4);  
//	
//	testTree[3].chlidren.push_back(5); 
//	testTree[3].chlidren.push_back(6); 
//	testTree[3].chlidren.push_back(7); 
//	
//	testTree[6].chlidren.push_back(8); 
//	testTree[6].chlidren.push_back(9); 
//	
//	testTree[7].chlidren.push_back(10); 
//	testTree[7].chlidren.push_back(11); 
//	
//	testTree[10].chlidren.push_back(12); 
//	testTree[10].chlidren.push_back(13); 
	
	 	
	
	cout<< "Please enter the number of queries:"<<endl;
    cin>>num_query;
    vector<pair<int,int> > query;
    cout<< "Please enter the "<< num_query<<" pair node of the query:"<<endl;
    for(int i = 0; i < num_query; i++)
    {
        cin>>x>>y;
        pair<int, int> a(x,y); 
    	query.push_back(a);
    }
    
    LCA l(testTree, num_nodes);
    map<pair<int, int>, int> ret = l.calLCA(query);
    
    cout << "Result: ";
    cout << endl;
    for (map<pair<int, int>, int>::iterator it = ret.begin(); it != ret.end(); it++){
    	cout << it->first.first << "," << it->first.second << "->" << it->second << "\t";   
    	cout << endl;
	}
        
 
    return 0;
}
